#!/bin/bash

# ComfyUI完全指南 - GitBook构建脚本
# 使用方法: ./build.sh [命令]
# 命令选项: install, serve, build, pdf, epub, mobi, clean

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

# 检查依赖
check_dependencies() {
    print_step "检查依赖..."
    
    # 检查Node.js
    if ! command -v node &> /dev/null; then
        print_error "Node.js 未安装，请先安装 Node.js"
        exit 1
    fi
    
    # 检查npm
    if ! command -v npm &> /dev/null; then
        print_error "npm 未安装，请先安装 npm"
        exit 1
    fi
    
    print_message "依赖检查完成"
}

# 安装GitBook和插件
install_gitbook() {
    print_step "安装GitBook CLI和插件..."
    
    # 安装GitBook CLI
    if ! command -v gitbook &> /dev/null; then
        print_message "安装GitBook CLI..."
        npm install -g gitbook-cli
    else
        print_message "GitBook CLI 已安装"
    fi
    
    # 安装插件
    print_message "安装GitBook插件..."
    gitbook install
    
    print_message "GitBook安装完成"
}

# 启动开发服务器
serve_book() {
    print_step "启动开发服务器..."
    print_message "服务器将在 http://localhost:4000 启动"
    print_message "按 Ctrl+C 停止服务器"
    gitbook serve
}

# 构建静态网站
build_book() {
    print_step "构建静态网站..."
    
    # 清理旧的构建文件
    if [ -d "_book" ]; then
        rm -rf _book
        print_message "清理旧的构建文件"
    fi
    
    # 构建
    gitbook build
    
    print_message "构建完成，输出目录: _book/"
}

# 生成PDF
generate_pdf() {
    print_step "生成PDF文档..."
    
    # 检查calibre是否安装
    if ! command -v ebook-convert &> /dev/null; then
        print_warning "calibre 未安装，无法生成PDF"
        print_message "请安装calibre: https://calibre-ebook.com/download"
        return 1
    fi
    
    gitbook pdf . ./comfyui-guide.pdf
    print_message "PDF生成完成: comfyui-guide.pdf"
}

# 生成EPUB
generate_epub() {
    print_step "生成EPUB文档..."
    
    # 检查calibre是否安装
    if ! command -v ebook-convert &> /dev/null; then
        print_warning "calibre 未安装，无法生成EPUB"
        print_message "请安装calibre: https://calibre-ebook.com/download"
        return 1
    fi
    
    gitbook epub . ./comfyui-guide.epub
    print_message "EPUB生成完成: comfyui-guide.epub"
}

# 生成MOBI
generate_mobi() {
    print_step "生成MOBI文档..."
    
    # 检查calibre是否安装
    if ! command -v ebook-convert &> /dev/null; then
        print_warning "calibre 未安装，无法生成MOBI"
        print_message "请安装calibre: https://calibre-ebook.com/download"
        return 1
    fi
    
    gitbook mobi . ./comfyui-guide.mobi
    print_message "MOBI生成完成: comfyui-guide.mobi"
}

# 清理构建文件
clean_build() {
    print_step "清理构建文件..."
    
    # 清理目录列表
    dirs_to_clean=("_book" "node_modules")
    files_to_clean=("*.pdf" "*.epub" "*.mobi")
    
    for dir in "${dirs_to_clean[@]}"; do
        if [ -d "$dir" ]; then
            rm -rf "$dir"
            print_message "删除目录: $dir"
        fi
    done
    
    for pattern in "${files_to_clean[@]}"; do
        if ls $pattern 1> /dev/null 2>&1; then
            rm -f $pattern
            print_message "删除文件: $pattern"
        fi
    done
    
    print_message "清理完成"
}

# 显示帮助信息
show_help() {
    echo "ComfyUI完全指南 - GitBook构建脚本"
    echo ""
    echo "使用方法: $0 [命令]"
    echo ""
    echo "可用命令:"
    echo "  install    安装GitBook和依赖"
    echo "  serve      启动开发服务器"
    echo "  build      构建静态网站"
    echo "  pdf        生成PDF文档"
    echo "  epub       生成EPUB文档"
    echo "  mobi       生成MOBI文档"
    echo "  all        生成所有格式"
    echo "  clean      清理构建文件"
    echo "  help       显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 install    # 安装依赖"
    echo "  $0 serve      # 启动开发服务器"
    echo "  $0 build      # 构建网站"
    echo "  $0 pdf        # 生成PDF"
    echo ""
}

# 生成所有格式
generate_all() {
    print_step "生成所有格式..."
    
    build_book
    generate_pdf
    generate_epub
    generate_mobi
    
    print_message "所有格式生成完成"
}

# 主函数
main() {
    case "${1:-help}" in
        "install")
            check_dependencies
            install_gitbook
            ;;
        "serve")
            check_dependencies
            serve_book
            ;;
        "build")
            check_dependencies
            build_book
            ;;
        "pdf")
            check_dependencies
            generate_pdf
            ;;
        "epub")
            check_dependencies
            generate_epub
            ;;
        "mobi")
            check_dependencies
            generate_mobi
            ;;
        "all")
            check_dependencies
            generate_all
            ;;
        "clean")
            clean_build
            ;;
        "help"|*)
            show_help
            ;;
    esac
}

# 执行主函数
main "$@"
